using Catalog.Application.CatalogItems.Commands;
using Catalog.Application.CatalogItems.Queries;
using Catalog.Application.Contracts.DTOs;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Catalog.HttpAPI.Host.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class CatalogItemController : ControllerBase
    {

        private readonly IMediator _mediator;

        public CatalogItemController(IMediator mediator)
        {
            _mediator = mediator;
        }
        
        [AllowAnonymous]
        [HttpGet("{id}")]
        public async Task<ActionResult<BrandProfileDto>> GetByIdAsync([FromRoute] GetCatalogItemByIdQuery query)
        {
            var catalogItem = await _mediator.Send(query);
            if (catalogItem == null)
            {
                return NotFound();
            }
            return Ok(catalogItem);
        }

        [AllowAnonymous]
        [HttpGet]
        public async Task<ActionResult<CatalogItemProfileDto>> GetCatalogItems([FromQuery] SearchCatalogItemQuery query)
        {
            var catalogItems = await _mediator.Send(query);
            if (catalogItems == null || !catalogItems.Any())
            {
                return NotFound("No catalog items found");
            }
            return Ok(catalogItems);
        }

        
        [Authorize(Roles="Admin")]
        [HttpPost("create")]
        public async Task<ActionResult> CreateCatalogItem([FromBody] CreateCatalogItemCommand command)
        {

            var result = await _mediator.Send(command);
            // Logic to create a new catalog item
            return Ok(result);
        }

        [Authorize(Roles="Admin")]
        [HttpPut("update")]
        public async Task<ActionResult> UpdateAsync([FromBody] UpdateCatalogItemCommand command)
        {
            var result = await _mediator.Send(command);

            return Ok(result);
        }

        [Authorize(Roles="Admin")]
        [HttpDelete("delete/{id}")]
        public async Task<IActionResult> DeleteAsync([FromRoute] Guid id)
        {
            var command = new DeleteCatalogItemCommand(id);
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [Authorize(Roles="Admin")]   
        [HttpPatch("change-name")]
        public async Task<IActionResult> ChangeNameAsync([FromBody] ChangeCatalogItemNameCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [Authorize(Roles="Admin")]
        [HttpPatch("change-description")]
        public async Task<IActionResult> ChangeDescriptionAsync([FromBody] ChangeCatalogItemDesciptionCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [Authorize(Roles="Admin")]
        [HttpPatch("change-price")]
        public async Task<IActionResult> ChangePriceAsync([FromBody] ChangeCatalogItemPriceCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [Authorize(Roles="Admin")]
        [HttpPatch("change-brand")]
        public async Task<IActionResult> ChangeBrandAsync([FromBody] ChangeCatalogItemBrandCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [Authorize(Roles="Admin")]
        [HttpPatch("change-category")]
        public async Task<IActionResult> ChangeCategoryAsync([FromBody] ChangeCatalogItemCategoryCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }
    }
}